"""
auth : ginger
    请求各接口 , 获取对应数据 , 最终获取所有报告信息
        get_all_workspace           :   获取所有工作空间
        get_project_by_workspace    :   根据工作空间获取所有项目
        _init_project_info_dict     :   初始化所有工作空间的 project_id 到全局变量中
        get_report_tables           :   根据 project_id 获取数据
        get_all_report_table_s      :   获取所有项目的统计数据 ( 遍历 project_id_list 获取数据 , 有值就保存 )
"""
import json
from common.do_log import do_log
from common.do_requests import request
from common.do_token import headers as request_headers
from dao.show_data import ReportData
from common.do_report_param import param_creator

project_info_dict = dict()
project_id_list = []


def get_all_workspace(ignore_s: tuple = ('demo',)):
    res = request(path=r'/workspace/list/userworkspace', method='get', headers=request_headers)
    result_dict = res.json().get('data')
    # print(result_dict)
    workspace_li = []
    for item in result_dict:
        flag = True
        for ignore in ignore_s:
            if item.get('name').count(ignore):
                flag = False
                continue
        if flag:
            workspace_li.append({'id': item.get('id'), 'name': item.get('name')})
    return workspace_li


def get_project_by_workspace(workspace_id):
    project_list, request_headers['WORKSPACE'] = [], workspace_id
    result = request(path=r'/project/listAll', method='GET', headers=request_headers).json().get('data')
    # print(result)
    for item in result:
        project_list.append({'id': item.get('id'), 'name': item.get('name')})
        project_info_dict[item.get('id')] = {'project': item.get('name'), 'workspace': item.get('workspaceName')}
    # print(project_info_dict)
    return project_list


def _init_project_info_dict(ignore_s: tuple = ('demo',)):
    if not project_info_dict:
        all_workspace = get_all_workspace(ignore_s=ignore_s)
        for item in all_workspace:
            workspace_id = item.get('id')
            project_list = get_project_by_workspace(workspace_id)
            for project in project_list:
                project_id_list.append(project.get('id'))
        do_log.info('project_id_list : ' + str(project_id_list))
        return project_id_list


def get_report_tables(project_id='c10ab475-facb-4e84-aa6c-c5c3d5388dec'):
    """

    根据 project_id 查询报告信息

    :param project_id: 默认值是 "易盘点" 下的 "易盘点" 项目

    """

    # _init_project_info_dict()

    param = param_creator(project_id)
    do_log.debug('接口请求参数 : ' + json.dumps(param))

    # 调用接口获取报告信息:
    res = request(path=r'/report/test/case/count/getReport', method='POST', headers=request_headers, param=param)
    result = res.json().get('data').get('tableDTOs')
    datas = []
    for item_result in result:
        sd = ReportData(item_result)
        data_s = sd.show(project_info_dict.get(project_id).get('workspace'),
                         project_info_dict.get(project_id).get('project'))
        if data_s:
            datas.append(data_s)
    do_log.debug('根据project_id获取数据成功 : ' + str(datas))
    return datas


def get_all_report_table_s(ignore_s: tuple = ('demo',)):
    project_id_s_list = _init_project_info_dict(ignore_s=ignore_s)
    all_data_s = []
    for project_id in project_id_s_list:
        data_s = get_report_tables(project_id=project_id)
        if data_s:
            all_data_s.extend(data_s)
    do_log.debug('根据 project_id 查询到记录为空的数据不会被记录 . ')
    do_log.debug('所有报告数据为 : ' + str(all_data_s))
    return all_data_s


if __name__ == '__main__':

    # print(get_all_workspace())
    # print(get_project_by_workspace('a763ddec-f815-476e-b816-a12d5312a9e4'))
    # print(_init_project_info_dict(('demo')))
    # get_report_tables(project_id='83e6df74-a6b7-4429-a2e3-b6fb1a9f6ab1')

    all_data = get_all_report_table_s()
    for i in all_data:
        print(i)
